<!doctype html>

<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>随机取人</title>
    <link rel="stylesheet" href="dist/style/weui.css" />
    <link rel="stylesheet" href="index.css?v=1.0" />
    <script src="dist/example/zepto.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript" src="dist/weui.js"></script>
</head>

<body oncontextmenu=self.event.returnValue=false>
    <div class="page">
        <div class="page__hd tohide" id="titletohide">
            <h1 class="page__title">实用工具：随机取人</h1>
        </div>
        <div class="page__bd">
            <div class="weui-cells__title">点击选择想要从中抽取的名单</div>
            <div class="weui-cells"><a class="weui-cell  weui-cell_access" onclick="openselector"
                    href="javascript:openselector()">
                    <div class="weui-cell__bd">
                        <p>选择名单</p>
                    </div>
                    <div class="weui-cell__ft">
                        <p id="selectedfile"></p>
                    </div>
                </a></div>
        </div>
        <div class="page__bd">
            <div class="weui-cells weui-cells_form">
                <div class="weui-cell weui-cell_active weui-cell_switch">
                    <div class="weui-cell__bd">去重</div>
                    <div class="weui-cell__ft">
                        <label for="switchCP" class="weui-switch-cp">
                            <input id="switchCP" class="weui-switch-cp__input" type="checkbox" />
                            <div class="weui-switch-cp__box"></div>
                        </label>
                    </div>
                </div>
                <div class="weui-cell weui-cell_active weui-cell_switch" id="isGirlSwitch" hidden="hidden">
                    <div class="weui-cell__bd">每组插入固定数量女生</div>
                    <div class="weui-cell__ft">
                        <label for="switchGirl" class="weui-switch-cp">
                            <input id="switchGirl" class="weui-switch-cp__input" type="checkbox" onclick='isGirl();' />
                            <div class="weui-switch-cp__box"></div>
                        </label>
                    </div>
                </div>
                <div id="isGirl" class="animate"><a class="weui-cell  weui-cell_access"
                        href="javascript:showPickerGirl();">
                        <div class="weui-cell__bd">
                            <p>每组女生数量</p>
                        </div>
                        <div class="weui-cell__ft pickertext">
                            <p id="selectedgirlnum"></p>
                        </div>
                    </a></div>
                <a class="weui-cell  weui-cell_access" href="javascript:showPickerGroup();">
                    <div class="weui-cell__bd">
                        <p>单次抽取组数</p>
                    </div>
                    <div class="weui-cell__ft pickertext">
                        <p id="selectedgroupnum"></p>
                    </div>
                </a> <a class="weui-cell  weui-cell_access" href="javascript:showPickerNum();">
                    <div class="weui-cell__bd">
                        <p>单组抽取个数</p>
                    </div>
                    <div class="weui-cell__ft pickertext">
                        <p id="selectednum"></p>
                    </div>
                </a>
            </div>
        </div>
        <div class="button-sp-area cell" id="show"></div>
        <div class="button-sp-area cell"><br />
        </div>
        <div class="page__bd page__bd_spacing">
            <div class="button-sp-area cell"></div>
            <div class="button-sp-area">
                <a href="javascript:chouqu();" class="weui-btn weui-btn_primary">抽取</a>
                <a href="javascript:clear();" class="weui-btn weui-btn_default">清空</a>
                <a href="javascript:viewlist();" class="weui-btn weui-btn_default">查看列表</a>
            </div>
        </div>
        <br />
        <div class="page__bd page__bd_spacing">
            <div class="weui-footer">
                <p class="weui-footer__text">Copyright &copy; 2020 北京航空航天大学197612班</p>
            </div>
        </div>
        <br />
    </div>
    <script>
        if (navigator.userAgent.indexOf("Html5Plus") > -1) {
            document.getElementById('titletohide').style.display = "none";//隐藏下载banner  
        }
        const names = ["周泽西", "边佳茜", "孙舟", "楚怡淳", "蒋韵澜", "武迪", "顾祚亚", "程浩宸", "张浩然",
            "孙宁", "荆煦添", "王鼎昊", "孙中大", "吴允瀚", "金驰", "王永岩", "刘志远", "王楷清", "张洪涛",
            "蔡贝林", "纪俊超", "林奕睿", "李东泽", "姚登宇", "袁京昌", "杨子灏", "曾显俊", "王亚荣", "李元龙"];

        const boys = ["武迪", "顾祚亚", "程浩宸", "张浩然",
            "孙宁", "荆煦添", "王鼎昊", "孙中大", "吴允瀚", "金驰", "王永岩", "刘志远", "王楷清", "张洪涛",
            "蔡贝林", "纪俊超", "林奕睿", "李东泽", "姚登宇", "袁京昌", "杨子灏", "曾显俊", "王亚荣", "李元龙"];

        const girls = ["周泽西", "边佳茜", "孙舟", "楚怡淳", "蒋韵澜",];
        var selist = [];
        var selistg = [];
        var selistb = [];
        var toselect;
        var elecount = 0;
        var grocount = 0;
        var girlnum = 1;
        var selected = '197612全体名单';
        var selectedid = '01';
        var selectedgroupnum = 1;
        var selectednum = 1;
        var numberGroup = [{
            label: '1',
            value: 0
        }, {
            label: '2',
            value: 1
        }, {
            label: '3',
            value: 2
        }, {
            label: '4',
            value: 3
        }, {
            label: '5',
            value: 4
        }, {
            label: '6',
            value: 5
        }, {
            label: '7',
            value: 6
        }, {
            label: '8',
            value: 7
        }, {
            label: '9',
            value: 8
        }, {
            label: '10',
            value: 9
        }];
        funTransitionHeight(document.getElementById('show'), 1);
        files = ['197612全体名单'];
        if (window.location.href.split('?')[1] == undefined) {
            document.getElementById('selectedfile').innerHTML = files[0];
            selectedid = '01';
            selected = files[0];
            toselect = names;
        } else if (window.location.href.split('?')[1] == '01') {
            document.getElementById('selectedfile').innerHTML = files[0];
            selectedid = '01';
            selected = files[0];
            toselect = names;
        } else if (window.location.href.split('?')[1].startsWith('id=')) {
            selectedid = 'id=' + window.location.href.split('?')[1].split('id=')[1];
            $.ajax({
                type: "post",
                url: "https://server.buaaer.club/getlist/",
                async: false,
                dataType: "json",
                data: {
                    "id": window.location.href.split('?')[1].split('id=')[1],
                },
                success: function (res) {
                    toselect = res.list;
                    selected = res.name;
                    document.getElementById('isGirlSwitch').style.display = 'none';
                    document.getElementById('selectedfile').innerHTML = res.name;
                },
                error: function (res) {
                    console.log(res);
                },
                complete: function (res) {
                }
            });
        } else {
            document.getElementById('selectedfile').innerHTML = files[0];
            selectedid = '01';
            selected = files[0];
            toselect = names
        }
        document.getElementById('selectedgroupnum').innerHTML = selectedgroupnum;
        document.getElementById('selectednum').innerHTML = selectednum;
        function openselector() {
            window.open("selector.html?" + selectedid, '_self');
        }

        function viewlist() {
            window.open('viewlist.html', '_self');
        }

        function chouqu() {
            if (document.getElementById('switchCP').checked && (selectedgroupnum * selectednum > toselect.length && (selectedgroupnum - 1) * selectednum >= toselect.length)) {
                weui.alert("您选择抽取的个数太多了");
                return;
            }
            if (document.getElementById('switchGirl').checked && document.getElementById('switchCP').checked) {
                if (girlnum * selectedgroupnum > girls.length) {
                    weui.alert("您选择抽取的女生数太多了");
                    return;
                } else if ((selectednum - girlnum) * selectedgroupnum > boys.length && (selectednum - girlnum) * (selectedgroupnum - 1) >= boys.length) {
                    weui.alert("男生不够啊亲");
                    return;
                }
            }
            if (document.getElementById('switchGirl').checked) {
                if (selectednum - girlnum < 0) {
                    weui.alert("每组抽取的人数需要大于等于女生数");
                    return;
                }
                for (var i = 0; i < selectedgroupnum; i++) {
                    girlSelectOne();
                }
            } else {
                for (var i = 0; i < selectedgroupnum; i++) {
                    normalSelectOne();
                }
            }
        }

        function clear() {
            grocount = 0;
            elecount = 0;
            selist = [];
            selistg = [];
            selistb = [];
            document.getElementById('show').innerHTML = '';
            funTransitionHeight(document.getElementById('show'), 250);
        }

        function showPickerGroup() {
            weui.picker(numberGroup, {
                defaultValue: [selectedgroupnum - 1],
                id: 1,
                onChange: function (result) {
                    selectedgroupnum = result[0].value + 1;
                    document.getElementById('selectedgroupnum').innerHTML = selectedgroupnum;
                },
                title: '单次抽取组数'
            });
        }

        function showPickerNum() {
            weui.picker(numberGroup, {
                id: 2,
                defaultValue: [selectednum - 1],
                onChange: function (result) {
                    selectednum = result[0].value + 1;
                    document.getElementById('selectednum').innerHTML = selectednum;
                },
                title: '每组抽取个数'
            });
        }

        function showPickerGirl() {
            weui.picker([{
                label: '1',
                value: 0
            }, {
                label: '2',
                value: 1
            }, {
                label: '3',
                value: 2
            }, {
                label: '4',
                value: 3
            }, {
                label: '5',
                value: 4
            }], {
                id: 3,
                defaultValue: [girlnum - 1],
                onChange: function (result) {
                    girlnum = result[0].value + 1;
                    document.getElementById('selectedgirlnum').innerHTML = girlnum;
                },
                title: '每组插入女生个数'
            });
        }

        function isGirl() {
            document.getElementById('selectedgirlnum').innerHTML = girlnum;
            if (document.getElementById('switchGirl').checked) {
                //document.getElementById('isGirl').hidden = false;
                document.getElementById('isGirl').style = "max-height: 56px;";
            } else {
                //document.getElementById('isGirl').hidden = true;
                document.getElementById('isGirl').style = "max-height: 0;";
            }
        }

        function normalSelectOne() {
            var onelist = "";
            if (document.getElementById('switchCP').checked) {
                if (elecount == 0 && grocount == 0) {
                    document.getElementById('show').innerHTML = '<div class="weui-cells__title">抽取结果</div>';
                }
                if (selist.length >= toselect.length) {
                    weui.alert("不能抽取更多组啦");
                    return;
                }
                var onelist = "";
                for (var i = 0; i < selectednum; i++) {
                    if (selist.length >= toselect.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * toselect.length);
                        if (selist.indexOf(r) == -1) {
                            flag = false;
                            selist.push(r);
                            if (selectedid == "02") {
                                onelist += toselect[r] + "\n"
                            }
                            onelist += toselect[r] + " ";
                            elecount += 1;
                        }
                    }
                }
            } else {
                if (elecount == 0 && grocount == 0) {
                    document.getElementById('show').innerHTML = '<div class="weui-cells__title">抽取结果</div>';
                }
                var onelist1 = [];
                for (var i = 0; i < selectednum; i++) {
                    if (onelist1.length >= toselect.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * toselect.length);
                        if (onelist1.indexOf(r) == -1) {
                            flag = false;
                            onelist1.push(r);
                            onelist += toselect[r] + " ";
                        }
                    }
                }
            }
            grocount += 1;
            var s = '<div class="weui-form-preview"><div class="weui-form-preview__hd"><div class="weui-form-preview__item"><label class="weui-form-preview__label">抽取组数</label><em class="weui-form-preview__value">第' + grocount + '组</em></div></div><div class="weui-form-preview__bd"><div class="weui-form-preview__item"><label class="weui-form-preview__label">内容</label><span class="weui-form-preview__value">' + onelist + '</span></div></div></div>';
            document.getElementById('show').innerHTML += s;
            funTransitionHeight(document.getElementById('show'), 250);
        }

        function funTransitionHeight(element, time) { // time, 数值，可缺省
            if (typeof window.getComputedStyle == "undefined") return;

            var height = window.getComputedStyle(element).height;

            element.style.transition = "none";    // 本行2015-05-20新增，mac Safari下，貌似auto也会触发transition, 故要none下~

            element.style.height = "auto";
            var targetHeight = window.getComputedStyle(element).height;
            element.style.height = height;
            element.offsetWidth = element.offsetWidth;
            element.offsetWidth;
            if (time) element.style.transition = "height " + time + "ms";
            element.style.height = targetHeight;
        }
        function girlSelectOne() {
            var onelist = "";
            if (document.getElementById('switchCP').checked) {
                if (elecount == 0 && grocount == 0) {
                    document.getElementById('show').innerHTML = '<div class="weui-cells__title">抽取结果</div>';
                }
                if (selistg.length > girls.length || selistb > boys.length) {
                    weui.alert("不能抽取更多组啦");
                    return;
                }
                if (selistg.length + girlnum > girls.length || selistb + selectednum - girlnum > boys.length) {
                    weui.alert("不能抽取更多组啦");
                    return;
                }
                var onelist = "";
                for (var i = 0; i < girlnum; i++) {
                    if (selistg.length >= girlnum.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * girls.length);
                        if (selistg.indexOf(r) == -1) {
                            flag = false;
                            selistg.push(r);
                            onelist += girls[r] + " ";
                            elecount += 1;
                        }
                    }
                }
                for (var i = 0; i < selectednum - girlnum; i++) {
                    if (selistb.length >= boys.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * boys.length);
                        if (selistb.indexOf(r) == -1) {
                            flag = false;
                            selistb.push(r);
                            onelist += boys[r] + " ";
                            elecount += 1;
                        }
                    }
                }
            } else {
                if (elecount == 0 && grocount == 0) {
                    document.getElementById('show').innerHTML = '<div class="weui-cells__title">抽取结果</div>';
                }
                var onelist1 = [];
                var onelist2 = [];
                for (var i = 0; i < girlnum; i++) {
                    if (selistg.length >= girls.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * girls.length);
                        if (onelist1.indexOf(r) == -1) {
                            flag = false;
                            onelist1.push(r);
                            onelist += girls[r] + " ";
                        }
                    }
                }
                for (var i = 0; i < selectednum - girlnum; i++) {
                    if (selistb.length >= boys.length) {
                        break;
                    }
                    var flag = true;
                    while (flag) {
                        var r = Math.floor(Math.random() * boys.length);
                        if (onelist2.indexOf(r) == -1) {
                            flag = false;
                            onelist2.push(r);
                            onelist += boys[r] + " ";
                        }
                    }
                }
            }
            grocount += 1;
            var s = '<div class="weui-form-preview"><div class="weui-form-preview__hd"><div class="weui-form-preview__item"><label class="weui-form-preview__label">抽取组数</label><em class="weui-form-preview__value">第' + grocount + '组</em></div></div><div class="weui-form-preview__bd"><div class="weui-form-preview__item"><label class="weui-form-preview__label">内容</label><span class="weui-form-preview__value">' + onelist + '</span></div></div></div>';
            document.getElementById('show').innerHTML += s;
            funTransitionHeight(document.getElementById('show'), 250);
        }


    </script>
    <script Language="JavaScript">
        function click() {
            if (event.button == 2 || event.button == 3) {
                weui.alert('复制用Control+C哦')
            }
        }
        //引号中提示可自定义
        document.onmousedown = click
    </script>
</body>

</html>